package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.PurchaseStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName: AlbumPurchaseStrategy
 * Package: com.atguigu.tingshu.user.strategy.impl
 * Description:
 *
 * @Author:
 * @Create: 2025/8/9 - 23:38
 * @Version: v1.0
 */

@Service("1003")
@Slf4j
public class VipPurchaseStrategy implements PurchaseStrategy {

    @Resource
    private UserVipServiceMapper userVipServiceMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Override
    public void purchase(UserPaidRecordVo userPaidRecordVo) {
        long count = userVipServiceMapper.selectCount(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo()));
        if (count > 0) {
            log.info("已经记录了vip的购买记录");
            return;
        }
        //获取到用户的信息，判断用户现在是否还有vip，如果有就是续期，没有就是开通
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        //购买的vip编号
        Long vipConfigId = userPaidRecordVo.getItemIdList().get(0);
        //获取到vip配置对象
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipConfigId);
        //vip时长
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //设置参数
        UserVipService userVipService = new UserVipService();
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        //  获取当前系统时间
        Date startTime = new Date();
        //判断用户是否还是VIP
        if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            startTime = userInfo.getVipExpireTime();
        }
        userVipService.setStartTime(startTime);
        //  获取过期时间
        Date expireTime = new DateTime(startTime).plusMonths(vipServiceConfig.getServiceMonth()).toDate();
        userVipService.setExpireTime(expireTime);
        userVipServiceMapper.insert(userVipService);
        //更新用户vip信息
        UserInfo userInfoUpt = new UserInfo();
        userInfoUpt.setId(userPaidRecordVo.getUserId());
        userInfoUpt.setIsVip(1);
        userInfoUpt.setVipExpireTime(expireTime);
        userInfoMapper.updateById(userInfoUpt);
    }
}
